package com.android.gtravelling;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class GTravellingActivity extends Activity {

	private ListView mainListView;
	private Employee[] employees;
	private ArrayAdapter<Employee> listAdapter;

	private ArrayList<Employee> planetList;

	private DBAdapter mDB;
	private Cursor mCursor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categories);
		Button btnAddNew = (Button) findViewById(R.id.btnVietTravel);
		btnAddNew.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iAdd = new Intent(v.getContext(), VietTravel.class);
				startActivity(iAdd);
			}
		});
		Button btnHotel = (Button) findViewById(R.id.btnHotels);
		btnHotel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iAdd = new Intent(v.getContext(), HotelTravel.class);
				startActivity(iAdd);
			}
		});
		/*mDB = new DBAdapter(this);
		mDB.open();

		Button btnAddNew = (Button) findViewById(R.id.ButtonAddNew);
		btnAddNew.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iAdd = new Intent(v.getContext(), AddActivity.class);
				startActivity(iAdd);
			}
		});

		mainListView = (ListView) findViewById(R.id.mainListView);

		mainListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View item,
							int position, long id) {
						Employee emp = listAdapter.getItem(position);
						emp.toggleChecked();
						EmployeeViewHolder viewHolder = (EmployeeViewHolder) item
								.getTag();
						viewHolder.getCheckBox().setChecked(emp.isChecked());
					}
				});
		WebserviceCaller caller = new WebserviceCaller();

		planetList = caller.selectAllUsers();
		listAdapter = new EmployeeArrayAdapter(this, planetList);

		mainListView.setAdapter(listAdapter);

		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.planets_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		spinner.setAdapter(adapter);
		spinner.setSelection(0);
*/
	}


	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			Toast.makeText(
					parent.getContext(),
					"The Option is " + parent.getItemAtPosition(pos).toString(),
					Toast.LENGTH_LONG).show();
			String act = parent.getItemAtPosition(pos).toString();
			if (act.equalsIgnoreCase("Detail") || act.equalsIgnoreCase("Edit")) {
				Intent i = new Intent(view.getContext(), EditActivity.class);
				startActivity(i);
			}

			if (act.equalsIgnoreCase("Delete")) {

				// String[] lstName = new String[planetList.size()];
				ArrayList<String> lstName = new ArrayList<String>();
				String dsd = "";
				for (int i = 0; i < planetList.size(); i++) {
					if (planetList.get(i).isChecked()) {
						lstName.add(planetList.get(i).getName());
						dsd += planetList.get(i).getName();
					}
				}
				Intent iDel = new Intent(view.getContext(),
						DeleteActivity.class);
				iDel.putStringArrayListExtra("listNameDel", lstName);
				startActivity(iDel);

			}

			if (act.equalsIgnoreCase("Exit")) {
				finish();
			}

		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	public void displayAMessage(String msg) {

		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Message");
		alertDialog.setMessage(msg);

		alertDialog.show();
	}

	public class EmployeeViewHolder {
		private CheckBox checkBox;
		private TextView textView;
		private TextView textView1;

		public EmployeeViewHolder() {
		}

		public EmployeeViewHolder(TextView textView, CheckBox checkBox) {
			this.checkBox = checkBox;
			this.textView = textView;
		}

		public EmployeeViewHolder(TextView textView, TextView textView1,
				CheckBox checkBox) {
			this.checkBox = checkBox;
			this.textView = textView;
			this.textView1 = textView1;

			this.textView.setOnLongClickListener(longLst);
		}

		public CheckBox getCheckBox() {
			return checkBox;
		}

		public void setCheckBox(CheckBox checkBox) {
			this.checkBox = checkBox;
		}

		public TextView getTextView() {
			return textView;
		}

		public TextView getTextView1() {
			return this.textView1;
		}

		public void setTextView(TextView textView) {
			this.textView = textView;
		}

		OnLongClickListener longLst = new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(v.getContext(), EditActivity.class);
				i.putExtra(Intent.EXTRA_TEXT, textView.getText().toString());
				startActivity(i);
				return false;
			}
		};

	}

	private class EmployeeArrayAdapter extends ArrayAdapter<Employee> {

		private LayoutInflater inflater;

		public EmployeeArrayAdapter(Context context, List<Employee> planetList) {
			super(context, R.layout.simplerow, R.id.rowTextView, planetList);
			// Cache the LayoutInflate to avoid asking for a new one each time.
			inflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// Employee to display
			Employee empl = (Employee) this.getItem(position);

			// The child views in each row.
			CheckBox checkBox;
			TextView textView;
			TextView textView1;
			// Create a new row view
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.simplerow, null);

				// Find the child views.
				textView = (TextView) convertView
						.findViewById(R.id.rowTextView);

				textView1 = (TextView) convertView
						.findViewById(R.id.rowTextView1);
				checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);

				// Optimization: Tag the row with it's child views, so we don't
				// have to
				// call findViewById() later when we reuse the row.
				convertView.setTag(new EmployeeViewHolder(textView, textView1,
						checkBox));

				// If CheckBox is toggled, update the planet it is tagged with.
				checkBox.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						CheckBox cb = (CheckBox) v;
						Employee employee1 = (Employee) cb.getTag();
						employee1.setChecked(cb.isChecked());
					}
				});
			}
			// Reuse existing row view
			else {
				// Because we use a ViewHolder, we avoid having to call
				// findViewById().
				EmployeeViewHolder viewHolder = (EmployeeViewHolder) convertView
						.getTag();
				checkBox = viewHolder.getCheckBox();
				textView = viewHolder.getTextView();
				textView1 = viewHolder.getTextView();
			}

			// Tag the CheckBox with the Planet it is displaying, so that we can
			// access the planet in onClick() when the CheckBox is toggled.
			checkBox.setTag(empl);

			// Display planet data
			checkBox.setChecked(empl.isChecked());
			textView.setText(empl.getName());
			textView1.setText(empl.getPhone());

			return convertView;
		}

	}

	public Object onRetainNonConfigurationInstance() {
		return employees;
	}
}